package com.example.job09redispubsub;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPubSub;

import javax.annotation.PostConstruct;

@Service
public class OrderLogService {

    @Autowired
    private JedisPool jedisPool;

    @PostConstruct
    public void sub() {
        new Thread(() -> {
            System.out.println("+++++++");
            Jedis jedis = new JedisPool("127.0.0.1", 6379).getResource();
            jedis.subscribe(new JedisPubSub() {
                @Override
                public void onMessage(String channel, String message) {

                    try {
                        Thread.sleep(1000); //模拟网络延迟
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("插入订单日志完成：" + message);
                }
            }, Constant.REDIS_CHANNEL_NAME);
        }).start();

    }

}
